
package com.compuvest.ajax;

// <editor-fold defaultstate="collapsed" desc="import">
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;

import java.util.logging.Level;
import java.util.logging.Logger;

import com.compuvest.web.SQLBean;
// </editor-fold>

@WebServlet(name="GetItems",loadOnStartup=1,urlPatterns={"/getitems"})
public class GetItems extends HttpServlet {

    private final static Logger log = Logger.getLogger(GetItems.class.getName());

    private SQLBean mysql;

// <editor-fold defaultstate="collapsed" desc="init()">
    @Override
    public void init(ServletConfig config) throws ServletException {
	super.init(config);

        mysql = (SQLBean)config.getServletContext().getAttribute("sb");
	if(mysql == null) {
            log.log(Level.SEVERE, "mysql=null");
        }
        log.log(Level.INFO, "Init");
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="processRequest()">

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {

        int catPage = getInt(request.getParameter("cp"), 0);

        int papeNo = getInt(request.getParameter("pg"), 0);
        int itemsOnPage = getInt(request.getParameter("iop"), 15);
        int sortID = getInt(request.getParameter("sr"), 3);
        int catID = getInt(request.getParameter("cid"), 0);
        int lowPrice = getInt(request.getParameter("lp"), 0);
        int hightPrice = getInt(request.getParameter("hp"), 0);
        int needChangeCat = getInt(request.getParameter("nc"), 1);

        String search = request.getParameter("q");
        String brandName = request.getParameter("bn");

        String mfr = request.getParameter("p0");
        String cond = request.getParameter("p1");
        String pref1 = request.getParameter("p2");
        String pref2 = request.getParameter("p3");
        String pref3 = request.getParameter("p4");
        String pref4 = request.getParameter("p5");
        String pref5 = request.getParameter("p6");

        String json = "";
        switch(catPage) {
            case 0:
//                json = "{'id':'100'}";
//                json = "{\"age\":\"25 years\",\"height\":\"170 cm\",\"weight\":\"120 kg\"}";
//                json = "{age:'25 years',height:'170 cm',weight:'120 kg'}";
//                json = "{person:{'age':'25 years','height':'170 cm','weight':'120 kg'}}";
                break;
            case 1:
            case 2:
                json = mysql.getClearanceItems(catPage, papeNo, itemsOnPage, sortID, catID, lowPrice, hightPrice,mfr, cond, pref1, pref2, pref3, pref4, pref5, search, needChangeCat);
                break;
            case 3:
                json = mysql.getBrandItems(catPage, papeNo, itemsOnPage, sortID, catID, lowPrice, hightPrice,mfr, cond, pref1, pref2, pref3, pref4, pref5, search, brandName, needChangeCat);
                break;
            case 11:
                json = mysql.getItemsPage(catPage, papeNo, itemsOnPage, sortID, catID, lowPrice, hightPrice,mfr, cond, pref1, pref2, pref3, pref4, pref5, search, needChangeCat);
                break;
            default:;
        }

        response.setContentType("text/html;charset=UTF-8");
        try(PrintWriter out = response.getWriter()) {
            out.print(json);
        }
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="getInt()">

     protected int getInt(String value, int defInt) {

        int integer = defInt;
        try {
            integer = Integer.parseInt(value);
        }
        catch(Exception ignore) { }

        return integer;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods.">
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }
    @Override
    public String getServletInfo() {
        return "GetItems";
    }

    @Override
    public void destroy() {
        log.log(Level.INFO, "Destroy");
    }
// </editor-fold>

}
